package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstArticleDTO;
import com.financing.wap.service.ICommonDstService;
import com.financing.wap.service.ICommonService;
import com.financing.wap.service.IDstArticleService;
import com.financing.wap.service.IDstUserVisitLogService;
import com.financing.wap.service.weixin.service.WeixinService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.ArticleInfoVO;
import com.financing.wap.web.controller.BaseController;

@Controller("dstArticleController")
@RequestMapping("/api/dstArticle")
public class DstArticleController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstArticleService dstArticleService;
	
	@Autowired
	private IDstUserVisitLogService dstUserVisitLogService;
	
    @Autowired
    private WeixinService weixinJincsServiceImpl;
    
    @Autowired
    private ICommonService commonService;
    @Autowired
    private ICommonDstService commonDstService;
    
    @RequestMapping("/searchArticle")
	@ResponseBody
	public ResultMap search(HttpServletRequest request, DstArticleDTO dstArticle){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("searchArticle begin .......");
			String openid=request.getParameter("openid");
			//参数  默认
			if(StringUtil.isNullOrEmptyStr(openid,dstArticle.getTitle())){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//文章类型
			dstArticle.setUserOpenId(openid);
			List<ArticleInfoVO> dstArticleList  = commonDstService.queryArticleForUser(dstArticle);
			for (int i = 0; i < dstArticleList.size(); i++) {
				ArticleInfoVO vo = dstArticleList.get(i);
				//日期格式化
				try {
					vo.setRiqi(DateUtil.analysisDate(DateUtil.formatDate(vo.getUpdateDate(),"yyyy-MM-dd"),true));
				} catch (Exception e) {
					log.error("detailDstUserArticle  set riqi error!",e);
				}
			}
			result.setAttribute("list",dstArticleList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("searchArticle end .......");
		}catch(Exception e){
			log.error("searchArticle error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/listDstArticle")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstArticleDTO dstArticle,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstArticle list begin .......");
			String openid=request.getParameter("openid");
			String articleType=request.getParameter("articleType");
			//参数  默认
			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//文章类型
			dstArticle.setCategories(articleType);
			dstArticle.setUserOpenId(openid);
			dstArticle.setBegin(pager.getStartIndex());
			dstArticle.setPageSize(pager.getPageSize());
			//上架的文章
			dstArticle.setArticleStatus("1");
			dstArticle.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			List<DstArticleDTO> dstArticleList = null;
			//只能查询出后台上传的文章    是否查询我的
			if("wode".equals(dstArticle.getCategories())){
				int count = commonService.myAllArticleInfoCount(dstArticle);
				pager.setTotalCount(count);
				if(count > 0){
					dstArticleList = commonService.myAllArticleInfo(dstArticle);
					//日期格式
					for (int i = 0; i < dstArticleList.size(); i++) {
						DstArticleDTO dstArticleDTO = dstArticleList.get(i);
						//日期格式化
						try {
							dstArticleDTO.setRiqi(DateUtil.analysisDate(DateUtil.formatDate(dstArticleDTO.getUpdateDate(),"yyyy-MM-dd"),true));
						} catch (Exception e) {
							log.error("detailDstUserArticle  set riqi error!",e);
						}
					}
				}else{
					dstArticleList = Collections.emptyList();
				}
			}else{
				dstArticle.setResource("1");
				int count = dstArticleService.queryListCount(dstArticle);
				pager.setTotalCount(count);
				if(count > 0){
					dstArticleList = dstArticleService.queryList(dstArticle);
					for (int i = 0; i < dstArticleList.size(); i++) {
						DstArticleDTO dstArticleDTO = dstArticleList.get(i);
						dstArticleDTO.setContent(null);
						//日期格式化
						try {
							dstArticleDTO.setRiqi(DateUtil.analysisDate(DateUtil.formatDate(dstArticleDTO.getUpdateDate(),"yyyy-MM-dd"),true));
						} catch (Exception e) {
							log.error("detailDstUserArticle  set riqi error!",e);
						}
					}
				}else{
					dstArticleList = Collections.emptyList();
				}
			}
			
			result.setAttribute("list",dstArticleList);
			
			result.setAttribute("pager", pager);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstArticle list end .......");
		}catch(Exception e){
			log.error("query dstArticle error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/detailDstArticle")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, DstArticleDTO dstArticle){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail dstArticle  begin .......");
			
			Integer id = dstArticle.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DstArticleDTO dto = dstArticleService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail dstArticle  end .......");
		}catch(Exception e){
			log.error("query detail dstArticle error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/***
	 * 访问和分享
	 * @param request
	 * @return
	 */
	@RequestMapping("/visitDstArticle")
	@ResponseBody
	public ResultMap shareDstArticle(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("visitDstArticle  begin .......");
			
			String id=request.getParameter("id");
			//type=1访问 2分享
			String type=request.getParameter("type");
			if(StringUtil.isNullOrEmptyStr(id,type)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			DstArticleDTO dto = dstArticleService.loadById(Integer.parseInt(id));
			if(dto.getId()!=null){
				if(type.equals("1")){
					dto.setViewCount(dto.getViewCount()+1);
				}else{
					dto.setShareCount(dto.getShareCount()+1);
				}
				DTOdateSetUtil.setUpdateInfo(dto);
				dstArticleService.update(dto);
			}
			logger.info("visitDstArticle   end .......");
		}catch(Exception e){
			log.error("visitDstArticle  error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
}
